import Vue from 'vue'
// 初始化 dayjs 相关配置
import dayjs from 'dayjs'
// 引入中文语言包
import 'dayjs/locale/zh-cn'
import relativeTime from 'dayjs/plugin/relativeTime'
import isBetween from 'dayjs/plugin/isBetween'

// 配置处理相对时间
dayjs.extend(relativeTime)
dayjs.extend(isBetween)

// 配置使用中文语言包
dayjs.locale('zh-cn')

// 过滤日期显示样式；如果 value 在当前年份内，显示月份；如果为年前则显示年份和月份
Vue.filter('relativeTime', value => {
  const isBetweenTime = dayjs(value).isBetween(new Date().getFullYear() + '-01' + '-01', dayjs(value).format('YYYY-MM-DD'))
  if (new Date().getTime() - value <= 3600000) {
    return dayjs(value).from(dayjs())
  } else if (isBetweenTime) {
    return dayjs(value).format('YYYY年M月D日')
  } else {
    return dayjs(value).format('M月D日')
  }
})

// 转换 Unix 时间戳为 -年-月-日
Vue.filter('dateTime', (value, format = 'YYYY-MM-DD:HH-mm') => {
  return dayjs(value).format(format)
})

// 毫秒
Vue.filter('filterMusicTime', value => {
  value = value.toString()
  var data = value.slice(0, value.length - 3)
  var minute = Math.floor(data / 60)
  var second = data - (minute * 60)
  minute = minute < 10 ? '0' + minute : minute
  second = second < 10 ? '0' + second : second
  return minute + ':' + second
})

// 秒
Vue.filter('filterMusicTimeMin', value => {
  var minute = Math.floor(value / 60)
  var second = Math.floor(value - (minute * 60))
  minute = minute < 10 ? '0' + minute : minute
  second = second < 10 ? '0' + second : second
  return minute + ':' + second
})
